package com.component;

import com.Listener.ActionDoneListener;
import com.dao.DeleteFarmer;
import com.dao.DeleteUser;
import com.dao.UpdateOrder;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class OrderManageComponent extends Box {
    final int WIDTH=1000;
    final int HEIGHT=600;

    private int FarmerID;

    JFrame jf = null;
    private JTable table;
    private JTableHeader jth;
    private JScrollPane scrollPane;
    private Object[][] info;

    public OrderManageComponent(JFrame jf,int FarmerID) {
        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图
        this.jf = jf;
        this.FarmerID = FarmerID;

        scrollPane = new JScrollPane();

        //组装表格
        //向数据库请求数据
        requestOrderData();
        //设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {
                //获取选中的条目
                int selectedRow = table.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1

                if (selectedRow==-1){
                    JOptionPane.showMessageDialog(jf,"请选择要更新的订单！");
                    return;
                }

                //防止误操作
                int result = JOptionPane.showConfirmDialog(jf, "确认要更新选中的订单吗？", "确认更新", JOptionPane.YES_NO_OPTION);
                if (result != JOptionPane.YES_OPTION){
                    return;
                }

                new UpdateOrder().updateOrder(1,(String)info[selectedRow][0]);

                JOptionPane.showMessageDialog(jf, "更新订单成功!");

                requestOrderData();
            }
        });
    }

    //请求数据
    public void requestOrderData() {
        try {
            //加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //获取连接
        String id = "sa";
        String pwd = "123456";
        String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=FarmProductSalesSystem";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection(url,id,pwd);
            String sql = "select * from ConsumerOrderView";
            stat = conn.createStatement(); //用于执行sql语句
            rs = stat.executeQuery(sql); //返回查询后的结果
            int count = 0;
            while(rs.next()){
                if(rs.getInt("FarmerID")==FarmerID) {
                    count++;
                }
            }
            rs = stat.executeQuery(sql);
            // 将查询获得的记录数据，转换成适合生成JTable的数据形式
            String[] ts = {"订单编号","消费者名称","产品编号","产品名称","产品数量","选购日期","总价格","订单状态"};
            info = new Object[count][8];
            count = 0;
            while(rs.next()){
                if(rs.getInt("FarmerID")==FarmerID){
                    info[count][0] = rs.getString("OrderID");
                    info[count][1] = rs.getString("ConsumerUsername");
                    info[count][2] = rs.getString("ProductID");
                    info[count][3] = rs.getString("ProductName");
                    info[count][4] = rs.getString("Quantity");
                    info[count][5] = rs.getString("PurchaseDate");
                    info[count][6] = rs.getString("TotalPrice");
                    info[count][7] = rs.getString("Status");
                    count++;
                }
            }
            this.table = new JTable(info,ts){
                @Override //设置不可编辑
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };
            //显示表头
            this.jth = this.table.getTableHeader();
            //设置滚动条
            this.scrollPane.getViewport().add(table);
            this.add(scrollPane);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("连接数据库失败！");
        }finally{
            //关闭coon
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //关闭stat
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //关闭rs
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
